package com.gitee.feizns.quickstart.jackson;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.gitee.feizns.dynamic.function.ChildrenFunction;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;

/**
 * json转换工具类
 * @author feizns
 * @since 2023/04/13
 */
public class JsonUtils {

    /**
     * 对象映射器
     */
    @Getter
    @Setter
    private static ObjectMapper objectMapper = new QuickstartObjectMapper();

    /**
     * 将任意对象转换为json
     * @param object 对象
     * @return {@link String}
     */
    @SneakyThrows
    public static String toJson(Object object) {
        return objectMapper.writeValueAsString(object);
    }

    /**
     * 解析为指定的对象
     * @param content 内容
     * @param typeReference 引用类型
     * @return {@link ObjectMapper#readValue(String, Class)}
     */
    @SneakyThrows
    public static <T> T parse(String content, Class<T> typeReference) {
        return objectMapper.readValue(content, typeReference);
    }

    /**
     * 解析为指定的对象
     * @param content 内容
     * @param javaType java类型
     * @return {@link T}
     */
    @SuppressWarnings("unchecked")
    @SneakyThrows
    public static <T> T parse(String content, JavaType javaType) {
        return (T) objectMapper.readValue(content, javaType);
    }

    /**
     * 解析
     * 解析为指定的对象
     * @param content 内容
     * @param typeReference 引用类型
     * @return {@link ObjectMapper#readValue(String, TypeReference)}
     */
    @SneakyThrows
    public static <T> T parse(String content, TypeReference<T> typeReference) {
        return objectMapper.readValue(content, typeReference);
    }

    /**
     * 转换为其他类型对象
     * @param original 源对象
     * @param typeReference 引用类型
     * @return {@link ObjectMapper#readValue(String, Class)}
     */
    public static <T> T convert(Object original, Class<T> typeReference) {
        return objectMapper.convertValue(original, typeReference);
    }

    /**
     * 转换为其他类型对象
     * @param original 源对象
     * @param typeReference 引用类型
     * @return {@link ObjectMapper#readValue(String, Class)}
     */
    public static <T> T convert(Object original, JavaType typeReference) {
        return objectMapper.convertValue(original, typeReference);
    }

    /**
     * 转换为其他类型对象
     * @param original 源对象
     * @param typeReference 引用类型
     * @return {@link ObjectMapper#readValue(String, Class)}
     */
    public static <T> T convert(Object original, TypeReference<T> typeReference) {
        return objectMapper.convertValue(original, typeReference);
    }

    /**
     * 解析json并获取节点
     * @param content 内容
     * @return {@link JsonNode}
     */
    public static JsonNode node(Object content) {
        return objectMapper.valueToTree(content);
    }

    /**
     * 是否是json
     * @param content 内容
     * @return boolean
     */
    public static boolean isJson(String content) {
        return !node(content).isMissingNode();
    }

    /**
     * 映射器
     * @return {@link ChildrenFunction}<{@link ObjectMapper}>
     */
    public static ObjectMapper mapper() {
        return objectMapper;
    }

    /**
     * 对象映射器
     * @return {@link ObjectMapper}
     */
    public static ObjectMapper objectMapper() {
        return objectMapper;
    }

    /**
     * 映射器
     * @return {@link ChildrenFunction}<{@link ObjectMapper}>
     */
    public static ChildrenFunction<ObjectMapper> mapperFu() {
        return new ChildrenFunction<>(objectMapper);
    }

    /**
     * 类型工厂
     * @return {@link TypeFactory}
     */
    public static TypeFactory typeFactory() {
        return objectMapper.getTypeFactory();
    }

}
